// console.log(`✅ ${__filename} 加载成功 - ${new Date().toISOString()}`);
console.log(`✅ 数据库成绩模块-加载成功 - ${new Date().toISOString()}`);

// 获取全部用户的成绩信息
// 获取当前账号所属的个人成绩信息
// const server_log = require('../utils/server_log');
// const NOW_DATE_Util = require('../utils/now_date');
// const pool = require('../config/mysql_db');
let poolPromise = require('../config/mysql_db'); // 引入Promise包装的连接池
 
class examine_scores_Model{
  // 获取所有成绩信息
  static async getAll_scores() {
    let sql = 'SELECT * FROM examine_scores'
    try{
      // 等待连接池Promise解析
      const pool = await poolPromise;
      const [rows] = await pool.query(sql);
      console.log('✅ 获取全部成绩信息成功--',new Date().toISOString());
      return rows;
    }catch (error) {
      console.log('❌ 获取全部成绩信息失败--',new Date().toISOString());
        console.error('获取所有成绩出错：', error);
        // let now_date = NOW_DATE_Util.getFormattedDateTime()
        //   let server_logData = {
        //     user_account: 0,
        //     user_name: '系统',
        //     date: now_date,
        //     sql_log: sql ,
        //     sqlMessage_log:error,
        //     userupdata:'无数据'
        // }
        // server_log.Write_server_log_to_JSON('../utils/server_log.json', server_logData);
        // server_log.Write_server_log_to_DB(server_logData);
    }
  }
 
  // 根据账号获取成绩信息
  static async getUser_scores(userAccount) {
      let sql = 'SELECT * FROM examine_scores WHERE user_account = ?'
    try{
      // 等待连接池Promise解析
      const pool = await poolPromise;
      const [rows] = await pool.query(sql, [userAccount]);
      console.log('✅ 账号'+userAccount+'获取个人成绩信息成功--',new Date().toISOString());
      return rows; // 返回多条记录
    }catch (error) {
      console.log('❌ 账号'+userAccount+'获取个人成绩信息失败--',new Date().toISOString());
      console.error('根据账号获取成绩信息错误:', error);
      // let now_date = NOW_DATE_Util.getFormattedDateTime()
      // let server_logData = {
      //     user_account: userAccount,
      //     user_name: '普通用户',
      //     date: now_date,
      //     sql_log: sql ,
      //     sqlMessage_log:error,
      //     userupdata:'无数据'
      // }
      // server_log.Write_server_log_to_JSON('../utils/server_log.json', server_logData);
      // server_log.Write_server_log_to_DB(server_logData);
    }
  }

  //上传成绩信息
  static async update_User_scores(scoreData) {
    let sql = 'INSERT INTO examine_scores(user_account, user_name, question_count, user_grade, exam_date,examine_true_num,examine_false_num) VALUES (?,?,?,?,?,?,?)'
    try{
      // 等待连接池Promise解析
      const pool = await poolPromise;
      const [result] = await pool.query(sql,[scoreData.user_account,scoreData.user_name,scoreData.question_count,scoreData.user_grade,scoreData.exam_date,scoreData.examine_true_num,scoreData.examine_false_num]);
      console.log('✅ 账号'+scoreData.user_account+'上传个人成绩信息成功--',new Date().toISOString());
      return {
        success: true,
        insertId: result.insertId,
        affectedRows: result.affectedRows,
        message: '成绩上传成功'
      };
    }catch (error) {
      console.log('❌ 账号'+scoreData.user_account+'上传个人成绩信息失败--',new Date().toISOString());
      console.error('账号'+scoreData.user_account+'上传成绩失败:', error);
        // const now_date = NOW_DATE_Util.getFormattedDateTime()
        // const server_logData = {
        //   user_account: scoreData.user_account,
        //   user_name: scoreData.user_name,
        //   date: now_date,
        //   sql_log: sql ,
        //   sqlMessage_log:error,
        //   userupdata:scoreData.user_account+'&'+scoreData.user_name+'&'+scoreData.question_count+'&'+scoreData.user_grade+'&'+scoreData.exam_date+'&'+scoreData.examine_true_num+'&'+scoreData.examine_false_num
        // }
        // console.log('报错信息：',server_logData)
        // server_log.Write_server_log_to_JSON('../utils/server_log.json', server_logData);
        // server_log.Write_server_log_to_DB(server_logData);
        // throw err;
        throw new Error(`数据库操作失败: ${error.message}`);
    }
  }  


  // 获取所有用户做题量数据信息
  static async getUser_scores_num_date(Today_start_date,Today_end_date,week_start_date,week_end_date,month_start_date,month_end_date) {
    let sql = 'CALL GetUserScoreSummaryCombined(?,?,?,?,?,?);'
    try{
      // 等待连接池Promise解析
      const pool = await poolPromise;
      const [rows] = await pool.query(sql,
        [
          Today_start_date,
          Today_end_date,
          week_start_date,
          week_end_date,
          month_start_date,
          month_end_date
        ]);
      console.log('✅ 获取用户存储过程统计信息成功--',new Date().toISOString());
      return rows; // 返回多条记录
    }catch (error) {
      console.log('❌ 获取用户存储过程统计信息失败--',new Date().toISOString());
      console.error('获取用户存储过程统计信息失败', error);
    }
  }



  }
module.exports = examine_scores_Model;



